﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Input;
using Structures;

namespace X3DXamlXna
{
    public interface ITouchComponent
    {

    }

    public class TouchComponent : GameComponent, ITouchComponent
    {
        private IEnumerable<X3dModel> models;

        public TouchComponent(Game game)
            : base(game)
        {
            game.Services.AddService(typeof(ITouchComponent), this);

        }

        public override void Initialize()
        {
            var modelC = Game.Services.GetService(typeof(IDrawableModel)) as IDrawableModel;
            models = modelC.models as IEnumerable<X3dModel>;
            base.Initialize();
        }

        public override void Update(GameTime gameTime) 
        {
            var camera = Game.Services.GetService(typeof(ICameraService)) as ICameraService;
            var mouse = Mouse.GetState();
            var ray = Game.GraphicsDevice.Viewport.GetRay(new Vector2(mouse.X, mouse.Y), camera.CurrentCamera.View, camera.CurrentCamera.Projection);

            X3dModel near = null;
            Nullable<float> nearnes = null;

            if (ray.HasValue)
            {
                foreach (var elem in models)
                {
                    Nullable<float> intersection = ray.Value.Intersects(elem.bounding);


                    if (intersection.HasValue && (nearnes == null || nearnes > intersection))
                    {
                        nearnes = intersection.Value;
                        near = elem;
                    }
                }
            }
            var effect = (Game.Services.GetService(typeof(IDrawableModel)) as IDrawableModel).effect;

            if (near!=null&& near.Touchable)
            {
                if (mouse.LeftButton == ButtonState.Pressed)
                {
                    System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
                    effect.LightingEnabled = true;
                    effect.PreferPerPixelLighting = true;
                    effect.DirectionalLight0.DiffuseColor = Color.Blue.ToVector3();
                    effect.DirectionalLight0.Direction = Vector3.Zero;
                    effect.DirectionalLight0.Enabled = true;
                    effect.DirectionalLight0.SpecularColor = Color.Red.ToVector3();


                }
                else
                {
                    System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Hand;
                    effect.LightingEnabled = true;
                    effect.PreferPerPixelLighting = true;
                    effect.EnableDefaultLighting();
                }

            }
            else
            {
                effect.LightingEnabled = true;
                effect.PreferPerPixelLighting = true;
                effect.EnableDefaultLighting();
                System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Arrow;
            }
            base.Update(gameTime);
        }
    }
}
